package com.example.demo.controller;

import com.example.demo.entity.Finance;
import com.example.demo.entity.Furniture;
import com.example.demo.entity.Order;
import com.example.demo.entity.Users;
import com.example.demo.service.FinanService;
import com.example.demo.service.FurnitureService;
import com.example.demo.service.OrderService;
import com.example.demo.util.JsonResult;
import com.example.demo.util.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys-order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    FinanService finanService;
    @Autowired
    FurnitureService furnitureService;
    @Autowired
    RedisTemplate redisTemplate;
    @GetMapping("/orders")
    public JsonResult<Users<Order>> orderList(@RequestParam("pagenum") int pageNum, @RequestParam("pagesize") int pageSize, @RequestParam("query")String query){
        Users<Order> users = orderService.orderPage((pageNum - 1) * pageSize, pageSize, query,null);
        return new JsonResult<>(200,users);
    }
    @DeleteMapping("/deleteOrder/{id}")
    public JsonResult<String> deleteOrder(@PathVariable int id){
        log.info("订单"+id);
        orderService.delete(id);
        return new JsonResult<String>(200,"删除成功");
    }
    //添加发货信息
    @PostMapping("/addFinance")
    public JsonResult<String> addFinance(@RequestBody Order order){
        if(order.getComment()==0){
            throw new ServiceException("用户还未提交订单");
        }
        Finance finance=new Finance();
        //设置发货信息
        finance.setBuyuser(order.getCreatedUser());
        finance.setPrice(order.getPrice()*order.getNumber());
        finance.setOrdername(order.getName());
        Date date=new Date();
        finance.setCreatedTime(date);
        finance.setModifiedTime(date);
        finance.setCreatedUser( (String) redisTemplate.opsForValue().get("username"));
        finance.setModifiedUser( (String) redisTemplate.opsForValue().get("username"));
        finanService.addFinance(finance);
        return new JsonResult<String>(200,"发货成功");
    }
    //添加订单
    @PostMapping("/addOrder")
    public JsonResult<String> addOrder(@RequestBody Furniture furniture){
        List<Furniture> furnitures = furnitureService.searchFurniture(furniture.getName());
        if(furnitures.get(0).getStatus()==1){
            throw new ServiceException("下单失败，该商品已下架");
        }
        log.info("addOrder"+ (String) redisTemplate.opsForValue().get("username"));
        orderService.add(furniture,  (String) redisTemplate.opsForValue().get("username"));
        return new JsonResult<String>(200,"加入订单成功");
    }
    @GetMapping("/order")
    public JsonResult<Users<Order>> order(@RequestParam("pagenum") int pageNum, @RequestParam("pagesize") int pageSize, @RequestParam("query")String query){
        Users<Order> users = orderService.orderPage((pageNum - 1) * pageSize, pageSize, query,(String) redisTemplate.opsForValue().get("username"));
        return new JsonResult<>(200,users);
    }
    @PutMapping("/updateOrder")
    public JsonResult<String> update(@RequestBody Order order){
        orderService.update(order);
        return new JsonResult<String>(200,"订单提交成功");
    }
    @PutMapping("/deOrder")
    public JsonResult<String> deOrder(@RequestBody Order order){
        if(order.getComment()==1){
            throw new ServiceException("订单已提交不能修改");
        }
        if(order.getNumber()==1){
            orderService.delete(order.getId());
        }
        orderService.update1(order);
        return new JsonResult<String>(200,"取消一件订单成功");
    }
}
